package com.up.vms.domain.vm.service;

import com.up.vms.domain.module.ActivityDetailDTO;
import com.up.vms.domain.module.ActivityDetailVO;
import com.up.vms.domain.vm.entity.ActivityDetail;
import com.up.vms.domain.vm.repository.ActivityDetailRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.Optional;

@Service
public class ActivityDetailService {

    @Autowired
    private ActivityDetailRepository activityDetailRepository;


    public Page<ActivityDetailVO> findAllByCaseDetail(ActivityDetailDTO detailDTO) {

        Sort sort = new Sort(Sort.Direction.DESC, "id");
        Pageable pageable = PageRequest.of(detailDTO.getPage(), detailDTO.getSize(), sort);

        return activityDetailRepository.getActivityDetails(detailDTO.getActivityId(), detailDTO.getStartTime(), detailDTO.getEndTime(), detailDTO.getUserName(), pageable);
    }

    public ActivityDetail getById(Long detailId) {
        Optional<ActivityDetail> optional = activityDetailRepository.findById(detailId);

        if (optional.isPresent()) {
            return optional.get();
        } else {
            return null;
        }
    }

    public ActivityDetail save(ActivityDetail activityDetail) {
        ActivityDetail newOne = activityDetailRepository.save(activityDetail);
        return newOne;
    }

    public ActivityDetail findByActivityIdAndUserId(Long activityId, Long userId) {
        ActivityDetail activityDetail = activityDetailRepository.findByActivityIdAndUserId(activityId, userId);
        return activityDetail;
    }
}
